package com.qkbb.admin.kuibu.qkbb.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.Servers.StepServers;
import com.qkbb.admin.kuibu.qkbb.fragment.Foot_fragment_first;
import com.qkbb.admin.kuibu.qkbb.fragment.Friend_fragment;
import com.qkbb.admin.kuibu.qkbb.fragment.More_fragment;
import com.qkbb.admin.kuibu.qkbb.fragment.Route_fragment;
import com.qkbb.admin.kuibu.qkbb.funcation.HttpURLConnHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.url.Url;


import org.json.JSONException;
import org.json.JSONObject;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

public class Theme extends FragmentActivity {
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    private String deviceid;
    private RequestQueue queue;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Handler handler;
    private String user_token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_theme);
        Intent intentserver=new Intent(this,StepServers.class);
        startService(intentserver);
        findview();
//        Window window = getWindow();
//        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//
//        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        sharedPreferences=getSharedPreferences("user", Context.MODE_PRIVATE);
        editor=sharedPreferences.edit();
        user_token=SDCardHelper.loadFileFromSdCard("user_token",getApplicationContext());
        if (user_token==null){
            getDeviceid();
        }else {
            Message message=new Message();
            message.what=1;
            handler.sendMessage(message);
        }
        RongyunIm();
        setOnCheck();

    }




    public void RongyunIm() {
        RongIM.init(getApplicationContext());
        String Token = "Qnee+JcUWv1nTIsQS+xu1X2vEtkQGOEscgmAUmZbVr9H3XZHP0y5yTztp9TADYovcdkBs0dwQotLuwF1G23k8KObMOUvDaTS";
        Log.e("aaa", "bbb");
        RongIM.connect(Token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                Log.e("失效", "失效");
            }

            @Override
            public void onSuccess(String s) {
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Log.e("friend error", errorCode + "");
                Log.e("cuowu", "meilianjieshang  " + errorCode);
            }
        });
    }
    public void getDeviceid(){
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        deviceid=telephonyManager.getDeviceId();
        queue= Volley.newRequestQueue(getApplicationContext());
        Log.e("唯一标识符", deviceid);
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte b[]=HttpURLConnHelper.loadByteFromURL(Url.REGISTER+deviceid,getApplicationContext());
                String s=new String(b);
                Jsoning(s);
            }
        }).start();

    }

    private void Jsoning(String s) {
        try {
            JSONObject jsonObject=new JSONObject(s);
            JSONObject jsonObject1=jsonObject.getJSONObject("data");
            JSONObject jsonObject2=jsonObject1.getJSONObject("LoginResult");
            user_token=jsonObject2.getString("user_token");
            Log.e("user_token", user_token);
            SDCardHelper.saveStringToSDCardCustomDir(user_token, "user_token");
            Log.e("them", SDCardHelper.loadFileFromSdCard("user_token", getApplicationContext()));
            editor.putInt("flage", 2);
            editor.commit();
            Message message=new Message();
            message.what=1;
            handler.sendMessage(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void findview(){
        radioGroup= (RadioGroup) findViewById(R.id.theme_radiogroup);
        radioButton= (RadioButton) findViewById(R.id.theme_rb_route);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==1){
                    first_fragment();
                }
            }
        };
    }

    private void first_fragment() {
        Route_fragment route_fragment=new Route_fragment();
        android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
        manager.beginTransaction()
                .replace(R.id.them_fragment,route_fragment)
                .commit();
    }

    public void setOnCheck(){
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.theme_rb_route:
                        radioButton.setChecked(true);
                        first_fragment();

                        break;
                    case R.id.theme_rb_footpath:
                        radioButton.setChecked(false);
                        Foot_fragment_first foot_fragment_first=new Foot_fragment_first();
                        android.support.v4.app.FragmentManager footmanger=getSupportFragmentManager();
                        footmanger.beginTransaction().replace(R.id.them_fragment,foot_fragment_first).commit();
                        break;
                    case R.id.theme_rb_friend:
                        radioButton.setChecked(false);
                        Friend_fragment friend_fragment=new Friend_fragment();
                        android.support.v4.app.FragmentManager friendmanger=getSupportFragmentManager();
                        friendmanger.beginTransaction().replace(R.id.them_fragment,friend_fragment).commit();
                        break;
                    case R.id.theme_rb_more:
                        radioButton.setChecked(false);
                        More_fragment more_fragment=new More_fragment();
                        android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
                        manager.beginTransaction()
                                .replace(R.id.them_fragment,more_fragment)
                                .commit();
                        break;
                }
            }


        });
    }

}
